﻿Imports System.IO

Public Class ReportFindTool
    Private tbPath As DataTable
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sql As String = "select ModelName,ReportFile,ReportDisName from ReportCfgMaster where 1=1 "
        If Not Me.ComboBox1.SelectedValue Is Nothing Then
            If Not String.IsNullOrEmpty(Me.ComboBox1.SelectedValue.ToString) Then
                sql += "  and ModelName='" + Me.ComboBox1.SelectedValue.ToString + "'"
            End If
        End If

        If Not String.IsNullOrEmpty(Me.TextBox1.Text.Trim) Then
            sql += " and ReportFile like '%" + Me.TextBox1.Text.Trim + "%'"
        End If
        Dim dt As DataTable = Exec(sql, E_ExecType.DataTable)
        Me.tbxDisplay.Clear()
        Me.tbxDisplay.AppendText(getDisListStr(System.Enum.Parse(GetType(mode), Me.ComboBox2.Text), dt))
        Me.tbxDisplay.AppendText(Me.tbxDisplay.Lines.LongLength - 2)
    End Sub

    Enum mode
        所有報表
        已完成報表
        未完成報表
    End Enum

    Private Function getDisListStr(ByVal typemode As mode, ByVal dt As DataTable) As String
        Dim fiter As String = "1=1"
        If Not Me.ComboBox1.SelectedValue Is Nothing Then
            If Not String.IsNullOrEmpty(Me.ComboBox1.SelectedValue.ToString) Then
                fiter = "ModelName='" + Me.ComboBox1.SelectedValue.ToString + "'"
            End If
        End If

        Dim str As New System.Text.StringBuilder
        str.AppendFormat("{0,-30}{1,-50}{2,-90}{3}", "ModelName", "ReportFIle", "ReportDisName", vbNewLine)
        Select Case typemode
            Case mode.已完成報表

                For Each row As DataRow In dt.Select(fiter)
                    str.AppendFormat("{0,-30}{1,-50}{2,-90}{3}", row("ModelName").ToString.Trim, row("ReportFile").ToString.Trim, row("ReportDisName").ToString.Trim, vbNewLine)
                Next
            Case mode.未完成報表
                Dim dir As DirectoryInfo
                For Each prow As DataRow In tbPath.Select(fiter)
                    If System.IO.Directory.Exists(prow("ReportsPath").ToString) Then
                        dir = New DirectoryInfo(prow("ReportsPath").ToString)
                        For Each pfile As System.IO.FileInfo In dir.GetFiles("*.rpt")
                            Dim rows() As DataRow = dt.Select("ModelName='" + prow("ModelName").ToString + "' and ReportFIle='" + pfile.Name + "'")
                            If rows.Length = 0 Then
                                str.AppendFormat("{0,-30}{1,-50}{2,-90}{3}", prow("ModelName").ToString.Trim, pfile.Name, "", vbNewLine)
                            End If
                        Next
                    End If
                Next
            Case mode.所有報表
                Dim dir As DirectoryInfo
                For Each prow As DataRow In tbPath.Select(fiter)
                    If System.IO.Directory.Exists(prow("ReportsPath").ToString) Then
                        dir = New DirectoryInfo(prow("ReportsPath").ToString)
                        For Each pfile As System.IO.FileInfo In dir.GetFiles("*.rpt")
                            Dim rows() As DataRow = dt.Select("ModelName='" + prow("ModelName").ToString + "' and ReportFIle='" + pfile.Name + "'")
                            If rows.Length > 0 Then
                                Dim row As DataRow = rows(0)
                                str.AppendFormat("{0,-30}{1,-50}{2,-90}{3}", row("ModelName").ToString.Trim, row("ReportFile").ToString.Trim, row("ReportDisName").ToString.Trim, vbNewLine)
                            Else
                                str.AppendFormat("{0,-30}{1,-50}{2,-90}{3}", prow("ModelName").ToString.Trim, pfile.Name, "", vbNewLine)
                            End If
                        Next
                    End If
                Next
        End Select
        Return str.ToString()
    End Function

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'TestDataSet.AuditLog' table. You can move, or remove it, as needed.
        prepareCbx()
        Dim sql As String = "SELECT listCode AS ModelName,ListA AS ReportsPath from BSC_OtherDefineB b inner join  BSC_OtherDefineA a on b.ItemNo=a.[ID] where a.ItemCode='ReportPath'"
        tbPath = Exec(sql, E_ExecType.DataTable)
    End Sub

    Private Sub prepareCbx()
        Dim sql As String = ""
        sql += "select  ListCode,ModeName+' ('+ListCode+')' as ModelName from BSC_OtherDefineB a" + vbNewLine
        sql += "inner join BSC_OtherDefineA b on a.ItemNO=b.id" + vbNewLine
        sql += "inner join Sys_ModeDefine c on ListCode=ModeNo and b.ItemCode='ReportPath'" + vbNewLine
        sql += "where c.parentid=0" + vbNewLine
         Me.ComboBox1.DataSource = Exec(sql, E_ExecType.DataTable)
        Me.ComboBox1.DisplayMember = "ModelName"
        Me.ComboBox1.ValueMember = "ListCode"
        Me.ComboBox1.SelectedIndex = -1
        Me.ComboBox2.Items.Clear()
        For Each name As String In System.Enum.GetNames(GetType(mode))
            Me.ComboBox2.Items.Add(name)
        Next
        Me.ComboBox2.SelectedIndex = 1
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.Button1.PerformClick()
        End If
    End Sub

End Class